package com.example.competionmanagebackend.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.competionmanagebackend.entity.Announcements;
import com.example.competionmanagebackend.service.AnnouncementsService;
import com.example.competionmanagebackend.utils.PageRequest;
import com.example.competionmanagebackend.utils.PageResult;
import com.example.competionmanagebackend.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/announcements")
public class AnnouncementsController {

    @Autowired
    private AnnouncementsService announcementsService;

    /**
     * 分页查询公告
     */
    @PostMapping("/getAnnouncementsPage")
    public ResponseEntity<Result<PageResult<Announcements>>> getAnnouncementsPage(@RequestBody PageRequest pageRequest) {
        Page<Announcements> page = announcementsService.getAnnouncementsPage(
                pageRequest.getCurrentPage(),
                pageRequest.getPageSize()
        );
        PageResult<Announcements> pageResult = new PageResult<>(page);
        return Result.success(pageResult);
    }

    /**
     * 新增或编辑公告
     */
    @PostMapping("/edit")
    public ResponseEntity<Result<Announcements>> editAnnouncement(@RequestBody Announcements announcement) {
        if (announcement.getId() == null) {
            // 新增公告
            announcementsService.insertAnnouncement(announcement);
        } else {
            // 编辑公告
            announcementsService.updateAnnouncement(announcement);
        }
        return Result.success(announcement);
    }

    /**
     * 删除公告
     */
    @GetMapping("/delete")
    public ResponseEntity<Result<String>> deleteAnnouncement(@RequestParam("id") Integer id) {
        announcementsService.deleteAnnouncement(id);
        return Result.success("删除成功");
    }
}
